/**
 * 
 */
package Control;

import java.util.ArrayList;

import javax.swing.JOptionPane;

import Entity.*;
import GUI.*;

/**
 * This class handle the event of the company and send the given information from the GUI's class to the server
 * it's create an event, cancel event, edit event and close an event
 * @author aviat
 */
public class HandleEventControl {

	private Event event;
	private ChooseEvent eveDataGui;
	private eventHandling eHandlinhGui;
	private CreateEvent creatEventGui;
	private EditEvent editEvGui;
	private RemoveCustomer removeCustomerGui;
	private ViewEvent viewEventGui;
	private MsgPackage eventHandelingMsg;
	/**
	 * Constructor
	 */	
	public HandleEventControl() {
		event = new Event();
		eveDataGui = new ChooseEvent();
		eHandlinhGui = new eventHandling();
		creatEventGui = new CreateEvent();
		editEvGui = new EditEvent();
		removeCustomerGui = new RemoveCustomer();
		viewEventGui = new ViewEvent();
		eventHandelingMsg = new MsgPackage();
	}
	/**
	 * This method send a request to server to get all event's date in DB
	 * 
	 */
	public ArrayList<Event> getEventDetails(){

		eventHandelingMsg.setOp_code(OP_CODE.GET_DATE_LIST);
		ScreenManager.getConnection().accept(eventHandelingMsg);
		
		Object o = ScreenManager.getConnection().cclient.getMessage();
		return (ArrayList<Event>) o;
		
	}

	/**
	 * This method send a request to servet in order to check if event exist in DB.
	 * 
	 */
	public boolean isEventExists(){
		
		eventHandelingMsg.setObject(event);
		eventHandelingMsg.setOp_code(OP_CODE.IS_EVENT_EXISTS);
		ScreenManager.getConnection().accept(eventHandelingMsg);
		
		Object o = ScreenManager.getConnection().cclient.getMessage();
		if((boolean) o)
    		return true;
    	return false;
	}
	/**
	 * This method send a request to server in order to check if event open.
	 * 
	 */
	public boolean isEventOpen(){
		
		eventHandelingMsg.setObject(event);
		eventHandelingMsg.setOp_code(OP_CODE.IS_EVENT_OPEN);
		ScreenManager.getConnection().accept(eventHandelingMsg);
		
		Object o = ScreenManager.getConnection().cclient.getMessage();
System.out.println("o: "+(boolean) o);
    	if((boolean) o)
    		return true;
    	return false;
	}
	/**
	 * This method set event's customer ID and date in the event class variable.
	 * 
	 */
	public void setEventData(){
	
		event.setG_b_names(eveDataGui.getGroom_bride_comboBox().getSelectedItem().toString());
		event.setEvetDate(eveDataGui.getEventDatecomboBox().getSelectedItem().toString());
		event.setCustomerID(Integer.parseInt(eveDataGui.getCustomerIDcomboBox().getSelectedItem().toString()));							
		event.setEvetPlace(eveDataGui.getPlace());
		creatEventGui.getPlaceTextField().setText(eveDataGui.getPlace());
		}
	/**
	 * This method sent to server a request to change event status to closed.
	 * @return true if event was closed successfully and false otherwise 
	 */
	public void closeEvent(){
		int ans = JOptionPane.showConfirmDialog (null, "Are you sure you want to close the event \nOf the customer : "+event.getG_b_names()+",on Date: "+event.getEvetDate()+"", "", JOptionPane.YES_NO_OPTION);
		System.out.println(ans);
		if(ans==0){
			eventHandelingMsg.setObject(event);
			eventHandelingMsg.setOp_code(OP_CODE.CLOSE_EVENT);
			ScreenManager.getConnection().accept(eventHandelingMsg);
			
			Object o = ScreenManager.getConnection().cclient.getMessage();
	
	    	if((boolean) o)
					JOptionPane.showMessageDialog(null, "Event was closed successfully", "", JOptionPane.INFORMATION_MESSAGE);
			else
				JOptionPane.showMessageDialog(null, "You are trying to close an event that is not yet paied for.\nEvent details: customers- "+event.getG_b_names()+" evevt date- "+event.getEvetDate()+"", "ERROR", JOptionPane.ERROR_MESSAGE);
		}
	}
	
	/**
	 * This method send a request to server to create new event
	 *@return true if event was created successfully and false otherwise
	 */
	public void createNewEvent(){
		if(creatEventGui.getPriceTextField().getText().toString().equals(null) || creatEventGui.getPlaceTextField().getText().toString().equals(null)
				||creatEventGui.geteTimetextField().getText().toString().equals(null)){
			JOptionPane.showMessageDialog(null, "Not all fields are filled, Please fill them and than press Create", "", JOptionPane.ERROR_MESSAGE);
			
		}
		else{
			event.setEventPricing(Integer.parseInt(creatEventGui.getPriceTextField().getText().toString()));
			event.setEvetPlace(creatEventGui.getPlaceTextField().getText().toString());
			event.seteTime(creatEventGui.geteTimetextField().getText().toString());
			eventHandelingMsg.setObject(event);
			eventHandelingMsg.setOp_code(OP_CODE.CREATE_EVENT);
			ScreenManager.getConnection().accept(eventHandelingMsg);
		
			Object o = ScreenManager.getConnection().cclient.getMessage();
	
			if((boolean) o){
				event.setEventPricing(null);
				event.setEvetPlace(null);
				JOptionPane.showMessageDialog(null, "Event was created successfully", "", JOptionPane.INFORMATION_MESSAGE);
			}
			else{
				JOptionPane.showMessageDialog(null, "This event is already exist in the system, Please try the 'EDIT EVENT' button", "ERROR", JOptionPane.ERROR_MESSAGE);
				
			}
			event.seteTime("");
			event.setEvetPlace("");
			event.setEventPricing(0);
		
		}
	}
	/**
	 * This method send a request to server to cancel event
	 *@return true if event was cancel successfully and false otherwise
	 */
	public boolean cancelEvent(){
		int ans = JOptionPane.showConfirmDialog(null, "You are about to delete an event of the customers: "+event.getG_b_names()+" at the Date: "+event.getEvetDate(), "", JOptionPane.YES_NO_OPTION);
		if(ans==1)
			return false;
		else{
			eventHandelingMsg.setObject(event);
			eventHandelingMsg.setOp_code(OP_CODE.CANCEL_EVENT);
			ScreenManager.getConnection().accept(eventHandelingMsg);
			
			Object o = ScreenManager.getConnection().cclient.getMessage();
	
			if((boolean) o)
				JOptionPane.showMessageDialog(null, "Event of customer: "+event.getCustomerID()+" at date:"+event.getEvetDate()+" has been canceled and deleted", "", JOptionPane.INFORMATION_MESSAGE);
			else{
				JOptionPane.showMessageDialog(null, "The Event Didn't canceled, Please try again later", "ERROR", JOptionPane.ERROR_MESSAGE);
				return false;
			}
		}
		return true;
	}
	/**
	 * This method send a request to server to check if the canceled event is the only event of the customer
	 * for open a remove customer GUI to delete him from the system
	 *@return true if it is the only event of the given customer and false otherwise
	 */
	public boolean checkNumberOfEvents(){
		
		eventHandelingMsg.setObject(event.getCustomerID());
		eventHandelingMsg.setOp_code(OP_CODE.GET_NUM_OF_EVENTS);
		ScreenManager.getConnection().accept(eventHandelingMsg);
		
		Object o = ScreenManager.getConnection().cclient.getMessage();
		
		if((boolean) o)
			return true;
		
		return false;
	}
	/**
	 * This method send a request to server to get customer details 
	 * 
	 */
	public ArrayList<Customer> CustomerDetailsToRemove(){
		ArrayList<Customer> cToRemove = new ArrayList<>();
		eventHandelingMsg.setObject(event.getCustomerID());
		eventHandelingMsg.setOp_code(OP_CODE.GET_CUSTOMER_DETAILS);
		ScreenManager.getConnection().accept(eventHandelingMsg);
		
		Object o = ScreenManager.getConnection().cclient.getMessage();
		Customer c = (Customer) o;
		cToRemove.add(c);
		System.out.println(c.getgFname());
		return cToRemove;
			
	}

	/**
	 * This method send a request to server to view event details
	 *
	 */
	public void viewEventDetails(){
		
		eventHandelingMsg.setObject(event);
		eventHandelingMsg.setOp_code(OP_CODE.VIEW_EVENT);
		ScreenManager.getConnection().accept(eventHandelingMsg);
	
		Object o = ScreenManager.getConnection().cclient.getMessage();

		event = (Event) o;
		viewEventGui.getEventDatetextField().setText(event.getEvetDate());
		viewEventGui.getEventPlacetextField().setText(event.getEvetPlace());
		viewEventGui.getPricetextField().setText(event.getEventPricing().toString());
		viewEventGui.getEventTimetextField().setText(event.geteTime().toString());
		if(event.isPaied())
			viewEventGui.getPaidtextField().setText("YES");
		else
			viewEventGui.getPaidtextField().setText("NO");
		eventHandelingMsg.setObject(event.getCustomerID());
		eventHandelingMsg.setOp_code(OP_CODE.GET_CUSTOMER_DETAILS);
		
		ScreenManager.getConnection().accept(eventHandelingMsg);
	
		Object res = ScreenManager.getConnection().cclient.getMessage();
		
		Customer c = (Customer) res;

		viewEventGui.getGroomNametextField().setText(c.getgFname()+" "+c.getgLname());
		viewEventGui.getBrideNametextField().setText(c.getbFname()+" "+c.getbLname());
		viewEventGui.getGroomPhonetextField().setText(c.getgPhone());
		viewEventGui.getBridePhonetextField().setText(c.getbPhone());
	
	}
	/**
	 * This method send a request to server to cancel event
	 *@return true if event was cancel successfully and false otherwise
	 */
	public boolean editEvent(){
		int flag =0;// flag will equal 1 if at list one filed will be selected and filled
		if(editEvGui.getChckbxNewDate().isSelected()){
			event.setEvetNewDate(editEvGui.getEdatePicker());
			if(event.getEvetNewDate().equals(null)){
				JOptionPane.showMessageDialog(null, "Please add a new Date for the event", "", JOptionPane.INFORMATION_MESSAGE);
				return false;
			}
			flag=1;
		}
		if(editEvGui.getChckbxNewPlace().isSelected()){
			event.setEvetPlace(editEvGui.getPlaceTextField().getText().toString());
			if(event.getEvetPlace().equals(null)){
				JOptionPane.showMessageDialog(null, "Please add a new Place for the event", "", JOptionPane.INFORMATION_MESSAGE);
				return false;
			}
			flag=1;
		}
		if(editEvGui.getChckbxNewTime().isSelected()){
			event.seteTime(editEvGui.getNewTimetextField().getText().toString());
			if(event.geteTime().equals(null)){
				JOptionPane.showMessageDialog(null, "Please add a new Time for the event", "", JOptionPane.INFORMATION_MESSAGE);
				return false;
			}
			flag=1;
		}

		if(editEvGui.getChckbxNewPrice().isSelected()){
			event.setEventPricing(Integer.parseInt(editEvGui.getPriceTextField().getText().toString()));
			if(event.getEventPricing()==null){
				JOptionPane.showMessageDialog(null, "Please add a new Price for the event", "", JOptionPane.INFORMATION_MESSAGE);
				return false;
			}
			flag=1;
		}
		if(editEvGui.getChckbxPaied().isSelected()){
			event.setPaied(true);
			flag=1;
		}
		if(flag==0){
			JOptionPane.showMessageDialog(null, "You didn't add any new information", "", JOptionPane.INFORMATION_MESSAGE);
			return false;
		}
		flag=0;
		eventHandelingMsg.setObject(event);
		eventHandelingMsg.setOp_code(OP_CODE.EDIT_EVENT);
		ScreenManager.getConnection().accept(eventHandelingMsg);
	
		Object o = ScreenManager.getConnection().cclient.getMessage();

		if((Integer) o==1)
			JOptionPane.showMessageDialog(null, "Event of customer: "+eveDataGui.getGroom_bride_comboBox().getSelectedItem().toString()+" at date:"+event.getEvetDate()+" has been paied before", "", JOptionPane.INFORMATION_MESSAGE);
		else if((Integer)o==2)	
			JOptionPane.showMessageDialog(null, "The Event Place of customer: "+eveDataGui.getGroom_bride_comboBox().getSelectedItem().toString()+" at date:"+event.getEvetDate()+" is already "+event.getEvetPlace()+"", "", JOptionPane.INFORMATION_MESSAGE);
		else if((Integer)o==3)	
			JOptionPane.showMessageDialog(null, "Event Date of customer: "+eveDataGui.getGroom_bride_comboBox().getSelectedItem().toString()+" is already "+event.getEvetNewDate()+"", "", JOptionPane.INFORMATION_MESSAGE);
		else if((Integer)o==4)	
			JOptionPane.showMessageDialog(null, "Event Price of customer: "+eveDataGui.getGroom_bride_comboBox().getSelectedItem().toString()+" at date:"+event.getEvetDate()+" is allredy "+event.getEventPricing()+"", "", JOptionPane.INFORMATION_MESSAGE);
		else if((Integer)o==6)	
			JOptionPane.showMessageDialog(null, "Event Time of customer: "+eveDataGui.getGroom_bride_comboBox().getSelectedItem().toString()+" at date:"+event.getEvetDate()+" is allredy "+event.geteTime()+"", "", JOptionPane.INFORMATION_MESSAGE);
		else if((Integer)o==5){	
			System.out.println("res = 5");
			if(!(event.getEvetNewDate().equals(""))){
				event.setEvetDate(event.getEvetNewDate());
				System.out.println("new date update");
			}
			JOptionPane.showMessageDialog(null, "Event of customer: "+eveDataGui.getGroom_bride_comboBox().getSelectedItem().toString()+" at date:"+event.getEvetDate()+" details has been edit successfully", "", JOptionPane.INFORMATION_MESSAGE);
		}
		else{
			JOptionPane.showMessageDialog(null, "The Event didn't created, Please try again later", "ERROR", JOptionPane.ERROR_MESSAGE);
			return false;
		}
		event.setEvetNewDate("");
		event.setEventPricing(0);
		event.setEvetPlace("");
		
		return true;
	}
	
	/**
	 * @return the eHandlinhGui
	 */
	public eventHandling geteHandlinhGui() {
		return eHandlinhGui;
	}



	/**
	 * @return the removeCustomerGui
	 */
	public RemoveCustomer getRemoveCustomerGui() {
		return removeCustomerGui;
	}


	/**
	 * @return the creatEvent
	 */
	public CreateEvent getCreatEvent() {
		return creatEventGui;
	}


	/**
	 * @return the editEv
	 */
	public EditEvent getEditEv() {
		return editEvGui;
	}



	/**
	 * @return the event
	 */
	public Event getEvent() {
		return event;
	}



	/**
	 * @return the eveData
	 */
	public ChooseEvent getEveData() {
		return eveDataGui;
	}
	/**
	 * @return the viewEventGui
	 */
	public ViewEvent getViewEventGui() {
		return viewEventGui;
	}
	

	

}
